package com.buddy.design.pattern._02singleton;

public class LazyDoubleCheckSingleton {

    private volatile static LazyDoubleCheckSingleton INSTANCE;

    private LazyDoubleCheckSingleton() {

    }


    public static LazyDoubleCheckSingleton getInstance() {
        //在被外部调用的时候才会被初始化
        if (INSTANCE == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (INSTANCE == null) {
                    INSTANCE = new LazyDoubleCheckSingleton();
                    //存在指令重排序问题，volatile
                    /**
                     * CPU执行时会转换为JVM指令
                     * 1.分配内存给这个对象；
                     * 2.初始化对象；
                     * 3.将初始化好的对象和内存地址建立关联，赋值；
                     * 4.用户初次访问
                     */
                }
            }
        }
        return INSTANCE;
    }


}